﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Wicresoft.Common;
using JDEduTrain.Framework.Business;
using JDEduTrain.Framework.ViewEntity;
using JDEduTrain.Framework.Entity;

public partial class Page_MySelectClassList : BaseAuthenticationPage
{

    public string SearchDefaultText
    {
        get
        {
            return "搜索 班级名称";
        }
    }

    public string SearchClause
    {
        get
        {
            if (ViewState["SearchClause"] == null)
                ViewState["SearchClause"] = "1=1";
            return ViewState["SearchClause"].ToString();
        }
        set
        {
            ViewState["SearchClause"] = value;
        }
    }
    protected DataTable DTUserPageList
    {
        get
        {
            if (ViewState["DTUserPageList"] != null)
            {
                return (DataTable)ViewState["DTUserPageList"];
            }
            return new DataTable();
        }
        set
        {
            ViewState["DTUserPageList"] = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        this.tbSearch.Attributes.Add("onfocus", string.Format("clearText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onblur", string.Format("insertText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onkeydown", "searchClick();");
        this.ShowLRLayoutButton = false;
        this.ShowUDLayoutButton = false;
        this.ShowSimpleLayoutButton = false;
        this.ShowAdvancedSearch = false;

        this.Title = "培训情况";
        if (!IsPostBack)
        {
            tbSearch.Text = SearchDefaultText;
            this.Order.OrderColumn = MemberTrainDetail.SQLCOL_PASSTIME;
            this.Order.OrderDirection = "DESC";
            DataTable dtDetail = BLTrain.GetTrainDetailByMemberID(CurrentUser.ID);
            float yeartotalScore = 0;
            float totalScore = 0;
            foreach (DataRow dr in dtDetail.Rows)
            {
                    if (dr["PassTime"].ToString().Equals(DateTime.Now.Year.ToString()))
                    {
                        try
                        {
                            yeartotalScore += float.Parse(dr["Score"].ToString());
                        }
                        catch (Exception)
                        {
                        }
                    }
                    totalScore += float.Parse(dr["Score"].ToString());
            }
            float scoreOrder = BLTrain.GetScoreOrderInDepartment(CurrentUser.ID);
            float turnsTotalScore = int.Parse(ConfigurationManager.AppSettings["TurnsTotalScore"]);
            float toFinishedScore = turnsTotalScore - totalScore;
            ltTitle.Text = DateTime.Now.Year.ToString() + "年度获得<span class='important'>" + yeartotalScore + "</span>学分；本单位学分排名：<span class='important'>" + scoreOrder + "</span>；迄今总共获得<span class='important'>" + totalScore + "</span>学分；" + "本轮待修<span class='important'>" + toFinishedScore + "</span>学分";

            BindData();
        }

    }

    protected override void Pager_PageChanged(object sender, EventArgs e)
    {
        BindData();
        base.Pager_PageChanged(sender, e);
    }

    protected override void Order_OrderChanged(object sender, EventArgs e)
    {
        BindData();
        base.Order_OrderChanged(sender, e);
    }

    private void BindData()
    {
        int recordCount = 0;
        DataTable dtAll = BLTrain.GetTrainDetailByMemberID(Pager.PageIndex,Pager.PageSize,  CurrentUser.ID, this.Order.OrderColumn +" " + this.Order.OrderDirection, ref recordCount, false);
        dtAll.Columns.Add("Organizers");
        foreach (DataRow dr in dtAll.Rows)
        {
            MemberTrainDetail detail = new MemberTrainDetail();
            detail.Init(dr);
            TrainClass trainClass = new TrainClass(detail.TrainClassID);
            TrainCredit credit = new TrainCredit(detail.CreditID);
            if (trainClass.Load())
            {
                dr["Organizers"] = trainClass.HostUnit;
            }
            else if (credit.Load())
            {
                dr["Organizers"] = credit.HostUnit;
            }

        }
        Pager.RecordCount = recordCount;
        rptUserInfo.DataSource = dtAll;
        rptUserInfo.DataBind();

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (tbSearch.Text != string.Empty && tbSearch.Text != SearchDefaultText)
        {
            SearchClause = string.Format("{0} LIKE N'%{1}%'", ClassMember.SQLCOL_CLASSNAME, tbSearch.Text);
        }
        else
            SearchClause = "1=1";
        BindData();
    }
    protected void btnAdvancedSearch_Click(object sender, EventArgs e)
    {
        BindData();

    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindData();
    }


}
